<?php
class Association_model extends MY_Model {
	function __construct() {
		parent::__construct('a_employee');
	}
	
	/**
     * 公用查询程序
     * $condition['category'] = ?  //按照社联员工类别搜索
     * $condition['association_id'] = ?  //按照社联员工编号
     * $condition['stu_num'] = ?  //按照社联员工学号查询
     * $condition['name'] = ?  //按照社联员工姓名查询
     * $condition['offset'] = ?
     * $condition['per_page'] = ?
     * @param $condition
     * @param $row_str_FLAG 仅对查询结果是单条的：若设为TRUE：单条自动转为字符串， 若设为FALSE：单条自动转为数组
     * @param $count_FLAG 若设为TRUE：计算查询结果数， 若设为FALSE：直接查询结果
     */
	function search_by_condition($condition, $row_str_FLAG = FALSE, $count_FLAG = FALSE) {
        $this->db->select('id, employee_id, a_category, a_name, a_gender, a_birth, a_stu_num, a_col, a_pro, a_phone, a_qq, confirm_FLAG, function_FLAG');

		if (isset($condition['association_id'])) {
            $this->db->where('employee_id', $condition['association_id']);
        }
        
        if (isset($condition['category'])) {
            $this->db->where('a_category', $condition['category']);
        }
        
		if (isset($condition['keywords'])) {
            $this->db->like('a_name', $condition['keywords']);
        }
        
		if (isset($condition['gender'])) {
            $this->db->where('a_gender', $condition['gender']);
        }
        
    	if (isset($condition['stu_num'])) {
            $this->db->where('a_stu_num', $condition['stu_num']);
        }
        
		if (isset($condition['col'])) {
            $this->db->where('a_col', $condition['col']);
        }
        
		if (isset($condition['pro'])) {
            $this->db->where('a_pro', $condition['pro']);
        }
        
		if (isset($condition['phone'])) {
            $this->db->where('a_phone', $condition['phone']);
        }
        
		if (isset($condition['qq'])) {
            $this->db->where('a_qq', $condition['qq']);
        }
        
    	if (isset($condition['confirm_FLAG'])) {
            $this->db->where('confirm_FLAG', $condition['confirm_FLAG']);
        }
        
    	if (isset($condition['function_FLAG'])) {
            $this->db->where('function_FLAG', $condition['function_FLAG']);
        }

        if (!$count_FLAG) {

            if (isset($condition['offset'])) {
                if (isset($condition['per_page'])) {
                    $this->db->limit($condition['per_page'], $condition['offset']);
                } else {
                    $this->db->limit(PER_PAGE, $condition['offset']);
                }

            }

            $query = $this->db->get('a_employee');
        	if ($query->num_rows() == 1) {
				if (!$row_str_FLAG) {
	            	return $query->result_array();
				} else {
					return $query->row_array();
            	}
			} elseif ($query->num_rows() > 1) {
				return $query->result_array();
			} else {
				return NULL;
			}
        } else {
            $this->db->from('a_employee');
            return $this->db->count_all_results();
        }
    }
    
    /**
     * 得到社联成员号是否为审核通过的
     * @param $society_id
     */
    function _get_association_confirm_flag($association_id) {
        $this->db->select("confirm_FLAG");
        $this->db->where("employee_id", $association_id);
        $query = $this->db->get($this->table_name);
        return $query->row_array();
    }
    
	/**
     * 得到社联成员号是否为部门功能号
     * @param $society_id
     */
    function _get_association_function_flag($association_id) {
        $this->db->select("function_FLAG");
        $this->db->where("employee_id", $association_id);
        $query = $this->db->get($this->table_name);
        return $query->row_array();
    }
	
	function _get_association_category_by_employee_id($employee_id) {
        $this->db->select("a_category");
        $this->db->where("employee_id", $employee_id);
        $query = $this->db->get($this->table_name);
        return $query->row_array();
    }
    
	/**
	 * 通过stu_num, phone, qq查询重复数据
	 * @param $stu_num
	 * @param $phone
	 * @param $qq
	 */
	
	function get_user_by_stu_num($stu_num) {
		$sql = "SELECT * FROM a_employee WHERE a_stu_num = ?";
		$data = $this->db->query($sql, array($stu_num));
		return $data->result();
	}
	
	function get_user_by_phone($phone) {
		$sql = "SELECT * FROM a_employee WHERE a_phone = ?";
		$data = $this->db->query($sql, array($phone));
		return $data->result();
	}
	
	function get_user_by_qq($qq) {
		$sql = "SELECT * FROM a_employee WHERE a_qq = ?";
		$data = $this->db->query($sql, array($qq));
		return $data->result();
	}
	
	function _get_association_name_by_employee_id($employee_id) {
        $this->db->select("a_name");
        $this->db->where("employee_id", $employee_id);
        $query = $this->db->get($this->table_name);
        $data = $query->row_array();
        return (array_key_exists('a_name', $data)) ? ($data['a_name']) : '';
    }
}